package cn.wolfcode.shop.controller;

import cn.wolfcode.shop.api.domain.*;
import cn.wolfcode.shop.api.page.PageResult;
import cn.wolfcode.shop.api.query.ProductQueryObject;
import cn.wolfcode.shop.api.service.*;
import cn.wolfcode.shop.api.util.JsonResult;
import cn.wolfcode.shop.api.vo.JsonResultVo;
import cn.wolfcode.shop.api.vo.ProductCatalogPropertyVo;
import cn.wolfcode.shop.api.vo.ProductVo;
import cn.wolfcode.shop.util.UploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Controller
@RequestMapping("product")
public class ProductController {
    @Value("${file.path2}")
    private String basepath;

    @Reference
    private IProductService productService;

    @Reference
    private IBrandService brandService;

    @Reference
    private ICatalogService catalogService;

    @Reference
    private ICatalogPropertyService catalogPropertyService;

    @Reference
    private IProductImageService iProductImageService;

    @Reference
    private IProductDescService productDescService;


    @RequestMapping("/getAll")
    @ResponseBody
    public List<Product> getAll() {
        return productService.getAll();
    }



    /*---------------------------------hqt---------------------------------------------------------------------*/
    /*---------------------------------hqt---------------------------------------------------------------------*/
    /*---------------------------------hqt---------------------------------------------------------------------*/
    /*---------------------------------hqt---------------------------------------------------------------------*/

    /**
     * 产品列表界面
     *
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("get")
    public String test(Model model, @ModelAttribute("qo") ProductQueryObject qo) {
        PageResult pageResult = productService.query(qo);
        model.addAttribute("pageResult", pageResult);
        model.addAttribute("catalogs", catalogService.selectAll());
        model.addAttribute("brands", brandService.getAll());
        return "/list/product";
    }

    /**
     * 产品列表进行更改
     *
     * @param product
     * @return
     */
    @RequestMapping("productUpdate")
    @ResponseBody
    public JsonResult productUpdate(Product product) {
        JsonResult jsonResult;
        try {
            productService.update(product);
            jsonResult = new JsonResult("更改成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult = new JsonResult(false, "更改失败");
        }
        return jsonResult;
    }

    /**
     * 查看产品内容
     *
     * @param model
     * @param productId
     * @return
     */
    @RequestMapping("/checkproduct")
    public String checkproduct(Model model, Long productId) {
        Product product = productService.get(productId);
        List<ProductImage> productImages = iProductImageService.get(productId);
        model.addAttribute("catalogs", catalogService.selectAll());
        model.addAttribute("brands", brandService.getAll());
        model.addAttribute("product", product);
        model.addAttribute("productImages", productImages);
        model.addAttribute("productDesc", productDescService.getDesc(productId));
        model.addAttribute("productCatalogValue", productService.selectProductCatalogPropertyValueByProductId(productId));
        return "list/product_list";
    }

    /**
     * 产品阐释列表
     *
     * @param model
     * @return
     */
    @RequestMapping("productlist")
    public String productlist(Model model) {
        List<Brand> brandList = brandService.getAll();
        List<Catalog> catalogList = catalogService.list();
        model.addAttribute("brands", brandList);
        model.addAttribute("catalogs", catalogList);
        return "/list/product_list";
    }

    /*--------------------------------hqt----------------------------------------------------------*/
    /*--------------------------------hqt----------------------------------------------------------*/
    /*--------------------------------hqt----------------------------------------------------------*/


    @RequestMapping("/inputPage")
    public String inputPage(Model model) {
        List<Brand> brandList = brandService.getAll();
        List<Catalog> catalogList = catalogService.list();
        model.addAttribute("brands", brandList);
        model.addAttribute("catalogs", catalogList);
        return "product/product_input";
    }


    @RequestMapping("/saveproduct")
    @ResponseBody
    public JsonResultVo saveProduct(ProductVo vo) {
        JsonResultVo jsonResult = new JsonResultVo();
        try {
            productService.saveProduct(vo);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setSuccess(false);
            jsonResult.setErrorMsg(e.getMessage());
        }
        return jsonResult;
    }

    @RequestMapping("/upload")
    @ResponseBody
    public String productImgUpload(MultipartFile file) {
        //String basePath = servletContext.getRealPath("/upload");
        String fileName = UploadUtil.upload(file, basepath);
        return "/" + fileName;
//                (new StringBuilder().append(fileName).toString());
        //append("G:/upload/").
    }

//    @RequestMapping("/productlist")
//    public String productList(ProductQueryObject qo, Model model) {
//        com.xmg.shop.api.query.PageResult pageResult = productService.query(qo);
//       model.addAttribute("pageResult", pageResult);
//        return "product/product_list";
//        return "";
//    }

    @RequestMapping("/propertyValue/get/{catalogId}")
    public String getCatalogProperty(Model model, @PathVariable("catalogId") Long catalogId) {
        List productCatalogPropertyList = new ArrayList();
        List catalogPropertyList = catalogPropertyService.getCatalogProperty(catalogId);
        ProductCatalogPropertyVo productCatalogPropertyVo;

        for (Iterator i$ = catalogPropertyList.iterator(); i$.hasNext(); productCatalogPropertyList.add(productCatalogPropertyVo)) {
            CatalogProperty catalogProperty = (CatalogProperty) i$.next();
            productCatalogPropertyVo = new ProductCatalogPropertyVo();
            productCatalogPropertyVo.setCatalogPropertyName(catalogProperty.getName());
            productCatalogPropertyVo.setType(catalogProperty.getType());
            List catalogPropertyValue = catalogPropertyService.getCatalogPropertyValue(catalogProperty.getId());
            productCatalogPropertyVo.setPropertyValueList(catalogPropertyValue);
        }

        model.addAttribute("productCatalogPropertyList", productCatalogPropertyList);
        return "product/catalog_property_panel";
    }
}
